package com.snapan.service.impl;

import com.snapan.dao.SystemConfigDao;
import com.snapan.entity.SystemConfig;
import com.snapan.service.SystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 系统配置表服务实现类
 */
@Service
@Transactional
public class SystemConfigServiceImpl implements SystemConfigService {

    @Autowired
    private SystemConfigDao systemconfigDao;

    @Override
    public SystemConfig getById(Long id) {
        return systemconfigDao.selectById(id);
    }

    @Override
    public List<SystemConfig> getAll() {
        return systemconfigDao.selectAll();
    }

    @Override
    public boolean add(SystemConfig systemconfig) {
        return systemconfigDao.insert(systemconfig) > 0;
    }

    @Override
    public boolean update(SystemConfig systemconfig) {
        return systemconfigDao.update(systemconfig) > 0;
    }

    @Override
    public boolean delete(Long id) {
        return systemconfigDao.deleteById(id) > 0;
    }


    @Override
    public Map<String, String> getAllConfigMap() {
        List<SystemConfig> configList = systemconfigDao.selectAll();
        return configList.stream()
                .collect(Collectors.toMap(
                        SystemConfig::getConfigKey,
                        SystemConfig::getConfigValue
                ));
    }
}
